package inline;

import android.text.TextUtils;
import android.util.Pair;

import com.bytedance.common.plugin.framework.util.c;
import com.bytedance.common.utility.Logger;
import com.bytedance.framwork.core.monitor.HttpResponseException;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.CRC32;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipException;

public class FileUtils {
    public static FileInputStream getFileInputStream(File file) {
        try {
            return new FileInputStream(file);
        } catch (FileNotFoundException e) {
            Logger.e("FileUtils", "catch", e);
            return null;
        }
    }

    public static boolean closeQuietly(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
                return true;
            } catch (IOException e) {
            }
        }
        return false;
    }

    public static byte[] inputStream2Bytes(InputStream inputStream) {
        if (inputStream == null) {
            return null;
        }

        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream(524288);
            outputStream2inputStream(inputStream, baos);
            return baos.toByteArray();
        } catch (IOException e) {
            Logger.e("FileUtils", "catch", e);
            return null;
        } finally {
            FileUtils.closeQuietly(baos);
        }
    }

    public static void outputStream2inputStream(InputStream is, OutputStream os) throws IOException {
        BufferedOutputStream v4 = null;
        BufferedInputStream v3 = null;
        int v0 = 524288;
        try {
            byte[] v1 = new byte[v0];
            if (!(is instanceof BufferedInputStream)) {
                v3 = new BufferedInputStream(is, 524288);
            }

            if (!(os instanceof BufferedOutputStream)) {
                v4 = new BufferedOutputStream(os, 524288);
            }

            while (true) {
                v0 = ((InputStream) v3).read(v1);
                if (v0 == -1) {
                    break;
                }

                ((OutputStream) v4).write(v1, 0, v0);
                ((OutputStream) v4).flush();
            }
            ((OutputStream) v4).flush();
        } catch (IOException v0_1) {
            v0_1.printStackTrace();
        } finally {
            FileUtils.closeQuietly(((Closeable) v3));
        }
        return;
    }

    public static c a(RandomAccessFile arg10) throws IOException {
        long v8 = 4294967295L;
        long v0 = 0;
        int v7 = 2;
        long v4 = arg10.length() - 22;
        if (v4 < v0) {
            throw new ZipException("File too short to be a zip file: " + arg10.length());
        }

        long v2 = v4 - 65536;
        if (v2 >= v0) {
            v0 = v2;
        }

        int v6 = Integer.reverseBytes(101010256);
        v2 = v4;
        do {
            arg10.seek(v2);
            if (arg10.readInt() == v6) {
                arg10.skipBytes(v7);
                arg10.skipBytes(v7);
                arg10.skipBytes(v7);
                arg10.skipBytes(v7);
                c v0_1 = new c();
                v0_1.b = (((long) Integer.reverseBytes(arg10.readInt()))) & v8;
                v0_1.a = (((long) Integer.reverseBytes(arg10.readInt()))) & v8;
                return v0_1;
            }
            --v2;
        }
        while (v2 >= v0);
        throw new ZipException("End Of Central Directory signature not found");
    }

    public static long a(RandomAccessFile arg12, c arg13) throws IOException {
        long v10 = 16384;
        CRC32 v3 = new CRC32();
        long v0 = arg13.b;
        arg12.seek(arg13.a);
        int v2 = ((int) Math.min(v10, v0));
        byte[] v4 = new byte[16384];
        for (v2 = arg12.read(v4, 0, v2); v2 != -1; v2 = arg12.read(v4, 0, ((int) Math.min(v10, v0)))) {
            v3.update(v4, 0, v2);
            v0 -= ((long) v2);
            if (v0 == 0) {
                break;
            }
        }

        return v3.getValue();
    }

    public static long getCrc(File file) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
        try {
            return FileUtils.a(randomAccessFile, FileUtils.a(randomAccessFile));
        } catch (Throwable throwable) {
            randomAccessFile.close();
            throw throwable;
        } finally {
            randomAccessFile.close();
        }
    }

    public static String d(String arg6) throws IOException {
        int v0_1;
        long v2;
        RandomAccessFile v1 = new RandomAccessFile(arg6, "r");
        try {
            v2 = v1.length() - 2;
            v1.seek(v2);
            v0_1 = v1.read() & 255 | (v1.read() & 255) << 8;
            if (v0_1 == 0) {

            } else {
                v2 -= ((long) v0_1);
                String v0_2 = null;
                try {
                    v1.seek(v2);
                    byte[] v2_1 = new byte[v0_1];
                    v1.read(v2_1);
                    v0_2 = new String(v2_1);
                } catch (Throwable v0) {

                } finally {
                    v1.close();
                }
                return v0_2;
            }
        } catch (Throwable v0) {
            v0.printStackTrace();
        }

        v1.close();
        String v0_2 = null;
        return v0_2;
    }

    public static byte[] b(InputStream arg4) throws IOException {
        ByteArrayOutputStream v0 = new ByteArrayOutputStream();
        byte[] v1 = new byte[8192];
        while (true) {
            int v3 = arg4.read(v1);
            if (-1 == v3) {
                break;
            }

            v0.write(v1, 0, v3);
        }

        if (arg4 != null) {
            arg4.close();
        }

        return v0.toByteArray();
    }

    public static byte[] a(String arg4, byte[] arg5, String arg6, String arg7, String arg8, boolean arg9, boolean arg10) throws Throwable {
        byte[] v0_4;
        try {
            LinkedList v1 = new LinkedList();
            URL v0_1 = arg10 ? new URL(UrlDemo.a(arg4, ((List) v1))) : new URL(arg4);
            URLConnection v0_2 = v0_1.openConnection();
            if ((arg10) && !((List) v1).isEmpty()) {
                Iterator<Pair<String, String>> v3 = ((List) v1).iterator();
                while (v3.hasNext()) {
                    Pair<String, String> v1_1 = v3.next();
                    if (v1_1 == null) {
                        continue;
                    }
                    ((HttpURLConnection) v0_2).setRequestProperty(v1_1.first, v1_1.second);
                }
            }

            if (arg9) {
                ((HttpURLConnection) v0_2).setDoOutput(true);
            } else {
                ((HttpURLConnection) v0_2).setDoOutput(false);
            }

            if (arg6 != null) {
                ((HttpURLConnection) v0_2).setRequestProperty("Content-Type", arg6);
            }

            if (arg7 != null) {
                ((HttpURLConnection) v0_2).setRequestProperty("Content-Encoding", arg7);
            }

            ((HttpURLConnection) v0_2).setRequestProperty("Accept-Encoding", "gzip");
            ((HttpURLConnection) v0_2).setRequestMethod(arg8);
            if (arg5 != null && arg5.length > 0) {
                DataOutputStream v1_2 = new DataOutputStream(((HttpURLConnection) v0_2).getOutputStream());
                v1_2.write(arg5);
                v1_2.flush();
                v1_2.close();
            }

            int v1_3 = ((HttpURLConnection) v0_2).getResponseCode();
            if (v1_3 == 200) {
                InputStream v1_4 = ((HttpURLConnection) v0_2).getInputStream();
                String v0_3 = ((HttpURLConnection) v0_2).getContentEncoding();
                if ((TextUtils.isEmpty(((CharSequence) v0_3))) || !v0_3.equalsIgnoreCase("gzip")) {
                    v0_4 = FileUtils.b(v1_4);
                } else {
                    GZIPInputStream v2 = new GZIPInputStream(v1_4);
                    v0_4 = FileUtils.b(((InputStream) v2));
                    v2.close();
                }

                if (v1_4 != null) {
                    v1_4.close();
                }

                return v0_4;
            }
            throw new HttpResponseException(v1_3, ((HttpURLConnection) v0_2).getResponseMessage());
        } catch (Throwable v0) {
            throw v0;
        }
    }

    public static void a(Closeable arg1) {
        if (arg1 != null) {
            try {
                arg1.close();
            } catch (IOException v0) {
                v0.printStackTrace();
            }
        }
    }
}
